package com.yunyan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunyan.entity.SchoolData;
import com.yunyan.mapper.SchoolMapper;
import com.yunyan.req.PageReq;
import com.yunyan.req.SchoolParam;
import com.yunyan.service.SchoolService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, SchoolData> implements SchoolService {

    @Override
    public IPage<SchoolData> pageQuery(PageReq pageReq, SchoolParam search) {
        return this.page(new Page<>(pageReq.getPage(), pageReq.getSize()),
                new LambdaQueryWrapper<SchoolData>()
                        .like(!StringUtils.isEmpty(search.getName()), SchoolData::getName, search.getName())
                        .like(!StringUtils.isEmpty(search.getAddress()), SchoolData::getAddress, search.getAddress())
                        .and(!StringUtils.isEmpty(search.getType()), wrapper -> {
                            wrapper.like(SchoolData::getType, search.getType())
                                    .or()
                                    .like(SchoolData::getLevel, search.getType());
                        })

                        .like(!StringUtils.isEmpty(search.getAttr()), SchoolData::getAttr, search.getAttr())
        );
    }

    @Override
    public List<SchoolData> listByName(Collection<String> nameList) {
        if (CollectionUtils.isEmpty(nameList)) {
            return new ArrayList<>();
        }
        return this.list(new LambdaQueryWrapper<SchoolData>()
                .in(SchoolData::getName, nameList)
        );
    }
}
